package com.cyss.greenball.world;

import org.jbox2d.collision.PolygonDef;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.EmbossMaskFilter;
import android.graphics.MaskFilter;
import android.graphics.Paint;

import com.cyss.greenball.exception.GameOverException;
import com.cyss.greenball.util.SystemUtil;

public class LineBarrier extends ElementObject {
	private float beginX, beginY, endX, endY, pixel;
	private Body body;
	private MaskFilter mEmboss;

	public LineBarrier(BallWorld world, float beginX, float beginY, float endX,
			float endY) {
		this(world, beginX, beginY, endX, endY, 2.0f);
	}

	public LineBarrier(BallWorld world, float beginX, float beginY, float endX,
			float endY, float pixel) {
		this(world, beginX, beginY, endX, endY, pixel, Color.RED);
	}

	public LineBarrier(BallWorld world, float beginX, float beginY, float endX,
			float endY, float pixel, int color) {
		super(world);
		this.beginX = beginX;
		this.beginY = beginY;
		this.endX = endX;
		this.endY = endY;
		this.pixel = pixel;
		paint.setStrokeWidth(pixel);
		paint.setColor(color);
//		paint.setDither(true);
//		paint.setStyle(Paint.Style.FILL);
//		paint.setStrokeJoin(Paint.Join.ROUND);
//		paint.setStrokeCap(Paint.Cap.ROUND);
//		mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f);
//		paint.setMaskFilter(mEmboss);
		instance();
	}

	private Vec2[] getRectangleVec() {
		float longRec = (float) Math.sqrt((endX - beginX) * (endX - beginX)
				+ (endY - beginY) * (endY - beginY));
		float wideRec = pixel;
		Vec2[] info = new Vec2[4];
		float offsetX = (endY - beginY) * wideRec / 2 / longRec
				/ SystemUtil.RATE;
		float offsetY = (endX - beginX) * wideRec / 2 / longRec
				/ SystemUtil.RATE;
		float tempX = (endX - beginX) / SystemUtil.RATE;
		float tempY = (endY - beginY) / SystemUtil.RATE;

		info[1] = new Vec2(tempX / 2 - offsetX, tempY / 2 + offsetY);
		info[0] = new Vec2(tempX / 2 + offsetX, tempY / 2 - offsetY);
		info[3] = new Vec2(-tempX / 2 + offsetX, -tempY / 2 - offsetY);
		info[2] = new Vec2(-tempX / 2 - offsetX, -tempY / 2 + offsetY);
		return info;
	}

	private float[] getLinearInfo() {
		float[] info = new float[3];
		float hx = (float) Math.sqrt((endX - beginX) * (endX - beginX)
				+ (endY - beginY) * (endY - beginY))
				/ SystemUtil.RATE / 2;
		float hy = pixel / SystemUtil.RATE / 2;
		float angle = (endY - beginY) / SystemUtil.RATE / hx;
		info[0] = hx;
		info[1] = hy;
		info[2] = angle;
		return info;
	}

	@Override
	public void instance() {
		PolygonDef shape = new PolygonDef();
		shape.density = 0.0f;
		shape.friction = 0.8f;
		shape.restitution = 0.3f;
		Vec2[] info = getRectangleVec();
		for (Vec2 v : info) {
			shape.addVertex(v);
		}

		BodyDef bodyDef = new BodyDef();
		bodyDef.position.set((endX + beginX) / 2 / SystemUtil.RATE,
				(endY + beginY) / 2 / SystemUtil.RATE);
		body = world.createBody(bodyDef);
		body.createShape(shape);
	}

	@Override
	public void onDraw(Canvas canvas) throws GameOverException {
		canvas.drawLine(beginX, beginY, endX, endY, paint);
	}

}
